package cn.znzz.cjm.service.impl;

import cn.znzz.cjm.config.RabbitMQConfig;
import cn.znzz.cjm.constants.CourseConstants;
import cn.znzz.cjm.doc.CourseDoc;
import cn.znzz.cjm.doc.QueryDoc;
import cn.znzz.cjm.domain.Course;
import cn.znzz.cjm.domain.CourseDetail;
import cn.znzz.cjm.domain.CourseMarket;
import cn.znzz.cjm.domain.CourseType;
import cn.znzz.cjm.dto.CourseDto;
import cn.znzz.cjm.feign.CourseDocFeignClient;
import cn.znzz.cjm.mapper.CourseDetailMapper;
import cn.znzz.cjm.mapper.CourseMapper;
import cn.znzz.cjm.mapper.CourseMarketMapper;
import cn.znzz.cjm.mapper.CourseTypeMapper;
import cn.znzz.cjm.query.CourseQuery;
import cn.znzz.cjm.service.ICourseService;
import cn.znzz.cjm.util.AjaxResult;
import cn.znzz.cjm.util.UserInfoUtil;
import cn.znzz.cjm.vo.UserInfo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.netflix.discovery.converters.Auto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.validation.ValidationUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenjiangmin
 * @since 2021-12-20
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseDocFeignClient docFeignClient;
    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private CourseDetailMapper detailMapper;
    @Autowired
    private CourseMarketMapper marketMapper;
    @Autowired
    private CourseTypeMapper typeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public AjaxResult save(CourseDto dto) {
        //获取当前用户的信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        UserInfo userInfo = userInfoUtil.getUserInfo(name);
        System.out.println(userInfo);
        /*
         * 课程  课程详情  课程市场信息是一对一的关系
         *  此时采用共享主键的方案  先保存course 返回保存的主键再保存 详情和市场信息
         */
        Course course = dto.getCourse();
        course.setTenantName(userInfo.getCompanyName());
        course.setTenantId(userInfo.getTid());
        course.setUserName(userInfo.getEmpName());
        courseMapper.insert(course);

        //保存市场信息
        CourseDetail detail = dto.getCourseDetail();
        detail.setId(course.getId());
        detailMapper.insert(detail);
        //保存详情
        CourseMarket courseMarket = dto.getCourseMarket();
        courseMarket.setId(course.getId());
        marketMapper.insert(courseMarket);
        return AjaxResult.me();
    }

    @Override
    public AjaxResult onLineCourse(List<Long> ids) {
        //排空
        assert ids!=null;
        for (Long id:ids) {
            Course course = courseMapper.selectById(id);
            CourseDoc doc = courseToDoc(course);
            CourseMarket courseMarket = courseMarketMapper.selectById(id);
            CourseDetail courseDetail = courseDetailMapper.selectById(id);
            BeanUtils.copyProperties(course, doc);
            BeanUtils.copyProperties(courseMarket, doc);
            BeanUtils.copyProperties(courseDetail, doc);
            //添加到es
            AjaxResult ajaxResult = docFeignClient.saveCourseDoc(doc);
            if (ajaxResult.isSuccess()) {
                //修改上线
                course.setStatus(CourseConstants.COURSE_STATUS_ONLION);
                courseMapper.updateById(course);
                //发送消息
                rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TOPIC,
                        "message.sms",
                        course.getName()
                );
                rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TOPIC,
                        "message.email",
                        course.getName()
                );
                rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME_TOPIC,
                        "message.system",
                        course.getName()
                );
            }
        }
        return AjaxResult.me();
    }

    @Override
    public AjaxResult offLineCourse(List<Long> ids) {
        assert ids!=null;
        for (Long id:ids) {
            Course course = courseMapper.selectById(id);
            CourseDoc doc = courseToDoc(course);
            CourseMarket courseMarket = courseMarketMapper.selectById(id);
            CourseDetail courseDetail = courseDetailMapper.selectById(id);
            BeanUtils.copyProperties(course, doc);
            BeanUtils.copyProperties(courseMarket, doc);
            BeanUtils.copyProperties(courseDetail, doc);
            AjaxResult ajaxResult = docFeignClient.delCourseDoc(doc);
            if (ajaxResult.isSuccess()) {
                //修改上线
                course.setStatus(CourseConstants.COURSE_STATUS_OFFLION);
                courseMapper.updateById(course);
            }
        }
        return AjaxResult.me();
    }
    @Override
    public AjaxResult queryCourses(QueryDoc docQuery) {
        AjaxResult result = docFeignClient.queryCourses(docQuery);
        return result;
    }

    private CourseDoc courseToDoc(Course course) {
        CourseDoc doc = new CourseDoc();
        CourseType type = typeMapper.selectById(course.getCourseTypeId());
        String name = type.getName();
        doc.setCourseTypeName(name);
        BeanUtils.copyProperties(course,doc);
        //将课程明细复制给doc
        CourseDetail courseDetail = courseDetailMapper.selectById(course.getId());
        BeanUtils.copyProperties(courseDetail,doc);
        //将市场复制给doc
        CourseMarket courseMarket = courseMarketMapper.selectById(course.getId());
        BeanUtils.copyProperties(courseMarket,doc);
        return doc;

    }

}
